package com.zpy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpy.pojo.Circulate;
import com.zpy.pojo.CountMonth;
import com.zpy.pojo.MainMenu;
import com.zpy.service.CirculateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("main")
public class CountMonthChart {
    @Autowired
    private CirculateService circulateService;

    @RequestMapping("mainMenu")
    public List<MainMenu> count(HttpSession session){
        Map<Integer,Integer>map=new HashMap<>();
        QueryWrapper<Circulate>qw=new QueryWrapper<>();
        String unit = (String) session.getAttribute("unit");
        qw.eq("uunit",unit);
        circulateService.list(qw).forEach(circulate -> {
            if (circulate.getCirculateTime()!=null){
                map.put((circulate.getCirculateTime().getMonth()+1),map.getOrDefault((circulate.getCirculateTime().getMonth()+1),0)+1);
            }
        });

        List<MainMenu>list=new ArrayList<>();
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            /*CountMonth countMonth = new CountMonth();
            countMonth.setMonth(String.valueOf(entry.getKey()));
            countMonth.setBorrowCount(entry.getValue());*/
            MainMenu mainMenu = new MainMenu();
            mainMenu.setType(String.valueOf(entry.getKey()));
            mainMenu.setMount(entry.getValue());
            list.add(mainMenu);

        }

        return list;
    }
}
